﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace CCL.Utility
{
    public static class ConverterUtility
    {
        static public int BufferSize = 1024;

        static public byte[] ToByteArrayFromString(string str)
        {
            MemoryStream stream = new MemoryStream(BufferSize);
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, str);

            byte[] rBytes = stream.GetBuffer();

            stream.Dispose();

            return rBytes;
        }

        static public string ToStringFromByteArray(byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(BufferSize);
            BinaryFormatter formatter = new BinaryFormatter();

            stream.Write(byteArray, 0, byteArray.Length);
            stream.Position = 0;

            string str = formatter.Deserialize(stream) as string;
            
            stream.Dispose();

            return str;
        }
    }
}
